<?php
namespace app\wechat\controller;
use app\model\Banner;
use app\model\Items;
use app\model\ItemsCate;

class ItemsController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function detail(){
        $id = $this->request->param('id');
        $item = Items::get(['id',$id]);
        $this->assign('item', $item);
        return $this->fetch();
    }

    /**
     * 获取商品列表接口
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists(){
        $search = $this->request->param();

        $queryFunc = function($query) use($search){
            $config = [
                'cate' => function($value) use ($query) {
                    $query->where('cate_id', $value);
                },
                'title' => function($value) use ($query) {
                    $query->whereLike('title', '%' . $value . '%');
                },
                'price' => function($value) use ($query) {
                    $query->where('org_price','<=', $value);
                },
                'price_range' => function($value) use ($query) {
                    $range = explode(':',$value);
                    if(($range[0] != '')&&($range[1] != '')){
                        $query->where('org_price','in', $range);
                    }elseif (($range[0] != '')&&($range[1] == '')){
                        $query->where('org_price','>=', $range[0]);
                    }else if(($range[0] == '')&&($range[1] != '')){
                        $query->where('org_price','<=', $range[1]);
                    }
                }
            ];

            foreach($search as $key=>$v) {
                // 执行对应搜索字段的处理方法
                if (array_key_exists($key, $config) && !empty($v)) {
                    $config[$key]($v);
                }
            }
        };

        if($this->request->param('order')){
            $order = ($this->request->param('order') == 'hot') ? 'sales desc' : 'org_price asc' ;
            $result = Items::where($queryFunc)->order($order)->paginate(20);
        }
        else{
            $result = Items::where($queryFunc)->paginate(20);
        }

        return msgReturn(true,'success',$result);
    }

    public function theme(){
        $type = $this->request->param('type');
        switch ($type){
            case '9.9':
                $type_result = 'price=9.9';
                $type_title = '9.9包邮';
                break;
            case '20':
                $type_result = 'price=20';
                $type_title = '20封顶';
                break;
            case 'hot':
                $type_result = 'order=hot';
                $type_title = '爆款疯抢';
                break;
            case 'min':
                $type_result = 'order=min';
                $type_title = '限时低价';
                break;
            default:
                $type_result = '';
                $type_title = '';
                break;
        }
        $cate_id = $this->request->param('cate');
        $cate = ItemsCate::column('id,cate_name');
        $this->assign('cate', $cate);
        $this->assign('cate_id', $cate_id);
        $this->assign('type_title',$type_title);
        $this->assign('type_result',$type_result);
        $this->assign('type',$type);
        return $this->fetch();
    }


}
